package club.kingyin.easycache.key;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class DefaultEasyCacheKey extends AbstractEasyCacheKey {
    @Override
    protected void setPrams() {

    }

    public DefaultEasyCacheKey() {

    }

    public DefaultEasyCacheKey(String methodName, String module, JSONObject prams, Set<String> exclude) {
        super.prams.putAll(prams);
        super.methodName = methodName;
        super.module = module;
        super.excludes = exclude;
    }

    @Data
    public static class Builder {
        private JSONObject prams = new JSONObject();
        private String methodName;
        private String module = DEFAULT_MODULE;
        private Set<String> excludes = new HashSet<>();

        private Map<String,Object> ancillary = new HashMap<>();


        public Builder addPram(String fieldName, Object fieldValue) {
            prams.put(fieldName, fieldValue);
            return this;
        }

        public Builder setPram(JSONObject pram) {
            prams = pram;
            return this;
        }

        public Builder setModule(String module) {
            this.module = module;
            return this;
        }

        public Builder setMethodName(String methodName) {
            this.methodName = methodName;
            return this;
        }

        public Builder ignorePram(String name) {
            this.excludes.add(name);
            return this;
        }

        public Builder copyWith(EasyCacheKey key) {
            this.methodName = key.getMethodName();
            this.module = key.getModule();
            this.prams = ((JSONObject) key.getPrams());
            this.excludes = ((AbstractEasyCacheKey) key).excludes;
            this.ancillary = ((AbstractEasyCacheKey) key).ancillary;
            return this;
        }

        public EasyCacheKey build() {
            DefaultEasyCacheKey cacheKey = new DefaultEasyCacheKey(methodName, module, prams, excludes);
            cacheKey.ancillary = this.ancillary;
            return cacheKey;
        }
    }
}
